using System;
using System.Data;
using System.Web.UI;
using System.Globalization;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using RealDolmen.SharePoint.Helpers;

namespace RealDolmen.SharePoint.ListFeatures
{
    public class ListFeatureAdder : UserControl
    {
        #region Fields
        protected Label lblEmptyFeaturesText;
        protected Label lblPageDescription;
        protected Literal ltrlNameHeading;
        protected Literal ltrlStatusHeading;
        private DataTable dtListFeatureDefinitionList;
        protected Repeater rptrFeatureList; 
        #endregion // Fields

        #region Event Overrides
        protected void BtnAddFeature_Click(object objSender, EventArgs evtargs)
        {
            Button button = (Button)objSender;
            Guid definitionGuid = new Guid(button.CommandArgument);

            ListFeatureDefinition definition =
                ListFeatureDefinition.Instantiate(definitionGuid);

            if (definition.CanBeAddedTo(SPContext.Current.List))
                definition.AddTo(SPContext.Current.List);

            // Allows the page to automatically close
            Page.ClientScript.RegisterStartupScript(
                this.GetType(),
                "SendReload",
                "window.opener.location.reload(true);",
                true);

            this.rptrFeatureList.DataSource = this.BuildFeatureListDataTable();
            this.rptrFeatureList.DataBind();

        }

        protected override void OnLoad(EventArgs evtargs)
        {
            base.OnLoad(evtargs);
            EnsureChildControls();

            this.rptrFeatureList.DataSource = this.FeatureListDataTable;
            this.rptrFeatureList.DataBind();
            if (this.FeatureListDataTable.Rows.Count == 0)
            {
                this.lblEmptyFeaturesText.Text = SPHttpUtility.HtmlEncode(SPResource.GetString("NoFeaturesText", new object[0]));
            }
        } 
        #endregion // Event Overrides

        #region Properties
        private DataTable FeatureListDataTable
        {
            get
            {
                if (this.dtListFeatureDefinitionList == null)
                {
                    this.dtListFeatureDefinitionList = this.BuildFeatureListDataTable();
                }
                return this.dtListFeatureDefinitionList;
            }
        } 
        #endregion // Properties

        #region DataTable Contruction
        private void AddItemsToTable(ref DataTable dataTable)
        {
            List<ListFeatureDefinition> defintions =
                ListFeatureDefinition.GetListFeaturesDefinitions(SPContext.Current.Web);

            foreach (ListFeatureDefinition listFeatureDefinition in defintions)
            {
                if (listFeatureDefinition.CanBeAddedTo(SPContext.Current.List))
                {
                    DataRow row = BuildDataRowFromFeatureDefinition(listFeatureDefinition, dataTable);
                    dataTable.Rows.Add(row);
                }
            }
        }

        private DataRow BuildDataRowFromFeatureDefinition(ListFeatureDefinition featureDefinition, DataTable dtblFeatures)
        {
            CultureInfo languageCulture = SPHelper.CurrentCultureInfo;
            DataRow row = dtblFeatures.NewRow();

            row["DefinitionId"] = featureDefinition.Id.ToString();
            row["ImageUrl"] = featureDefinition.GetImageUrl(languageCulture);
            row["ImageAlt"] = featureDefinition.GetImageAltText(languageCulture);
            row["Title"] = featureDefinition.GetTitle(languageCulture);
            row["Description"] = featureDefinition.GetDescription(languageCulture);

            return row;
        }

        private DataTable BuildFeatureListDataTable()
        {
            string[] strArray = new string[] { "DefinitionId", "ImageUrl", "ImageAlt", "Title", "Description" };
            DataTable dtblFeatureList = new DataTable();

            foreach (string col in strArray)
            {
                dtblFeatureList.Columns.Add(
                    new DataColumn(col, typeof(string)));
            }

            this.AddItemsToTable(ref dtblFeatureList);

            dtblFeatureList.Locale = CultureInfo.CurrentUICulture;
            dtblFeatureList.DefaultView.Sort = "Title ASC";
            return dtblFeatureList;
        }
        #endregion // DataTable Contruction
    }
}
